package com.ruike.alisurface.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.bean.GoodsBean;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.imageLoader.ImageLoader;

import java.io.File;
import java.util.List;
import java.util.Locale;

/**
 * 商品展示适配器
 */
public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

    Context context;
    List<GoodsBean> list;

    public GoodsListAdapter(Context context, List<GoodsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setData(List<GoodsBean> list) {
        L.i("商品数据", "刷新数据");
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
        ImageView imageView = holder.productImageImgv;
        if (imageView != null) {
            ImageLoader.clearImage(context, imageView);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        GoodsBean goodsBean = list.get(i);
        viewHolder.nullProductImgv.setVisibility(goodsBean.getCount() <= 0 ? View.VISIBLE : View.GONE);
//        viewHolder.currentNumberImgv.setText(String.valueOf(goodsBean.getCount()));
        if (!TextUtils.isEmpty(goodsBean.getProductimg())) {
            String name = goodsBean.getProductimg().substring(goodsBean.getProductimg().lastIndexOf("/"));
            File myFile = new File(Constant.path, name);
            if (!myFile.exists()) {
                L.i("goods====文件不存在 加载网络=" + goodsBean.getProductimg());
                // 加载图片
                ImageLoader.loadImage(context, goodsBean.getProductimg(), viewHolder.productImageImgv);
            } else {
                // 加载图片
                ImageLoader.loadImage(context, myFile.getAbsolutePath(), viewHolder.productImageImgv);
            }
        } else {
            ImageLoader.loadImage(context, R.drawable.img_zanwimg, viewHolder.productImageImgv);
        }
        // 设置商品名称
        viewHolder.productNameTv.setText(goodsBean.getProductname());
        // 设置价格
        viewHolder.productPriceTv.setText(String.format(Locale.ENGLISH, "%.2f", goodsBean.getPrice()));
        // 设置货道号
        StringBuilder slotNumberSb = new StringBuilder();
        int index = goodsBean.getSlotIndex();
        for (int j = 0; j < 3 - (String.valueOf(index).length()); j++) {
            slotNumberSb.append("0");
        }
        slotNumberSb.append(index);
        viewHolder.slotNumberTv.setText(slotNumberSb.toString());
        // 设置item点击监听
        viewHolder.itemView.setOnClickListener(view -> {
            if (onGoodsItemClickListener != null) {
                onGoodsItemClickListener.onGoodsClick(viewHolder.productImageImgv, i, goodsBean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        //        TextView currentNumberImgv;
        ImageView productImageImgv;
        ImageView nullProductImgv;
        TextView productNameTv;
        TextView slotNumberTv;
        TextView productPriceTv;

        public ViewHolder(View itemView) {
            super(itemView);
//            currentNumberImgv = itemView.findViewById(R.id.itemGoods_currentNumber_tv);
            productImageImgv = itemView.findViewById(R.id.itemGoods_productImage_imgv);
            nullProductImgv = itemView.findViewById(R.id.itemGoods_nullProduct_imgv);
            productNameTv = itemView.findViewById(R.id.itemGoods_productName_tv);
            slotNumberTv = itemView.findViewById(R.id.itemGoods_slotNumber_tv);
            productPriceTv = itemView.findViewById(R.id.itemGoods_productPrice_tv);
        }
    }

    public OnGoodsItemClickListener onGoodsItemClickListener;

    public void setOnGoodsItemClickListener(OnGoodsItemClickListener onGoodsItemClickListener) {
        this.onGoodsItemClickListener = onGoodsItemClickListener;
    }

    /**
     * 商品列表项点击监听
     */
    public interface OnGoodsItemClickListener {
        void onGoodsClick(ImageView imageView, int index, GoodsBean clickBean);
    }

}
